package org.chen.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.chen.common.exception.ExceptionCodeEnum;
import org.chen.common.page.CommonPage;
import org.chen.common.request.CreateTravelOrderRequest;
import org.chen.common.request.OrderRefundApplyRequest;
import org.chen.common.request.PageParamRequest;
import org.chen.common.request.TravelOrderRequest;
import org.chen.common.response.CommonResult;
import org.chen.common.response.OrderTravelDetailResponse;
import org.chen.common.response.PreTravelOrderResponse;
import org.chen.common.response.StoreTravelOrderDetailInfoResponse;
import org.chen.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Slf4j
@RestController("StoreOrderFrontController")
@RequestMapping("api/order")
@Tag(name = "订单", description = "订单")
public class StoreOrderTravelController {

    @Autowired
    private OrderService orderService;

    /**
     * 预下单(旅游)
     */
    @Operation(summary = "预下单(旅游)")
    @RequestMapping(value = "/pre/order/travel", method = RequestMethod.POST)
    public CommonResult<Map<String, Object>> preOrderTravel(@RequestBody @Validated TravelOrderRequest request) {
        return CommonResult.success(orderService.preOrderTravel(request));
    }

    /**
     * 加载预下单(旅游)
     */
    @Operation(summary = "加载预下单(旅游)")
    @RequestMapping(value = "load/pre/travel/{preOrderNo}", method = RequestMethod.GET)
    public CommonResult<PreTravelOrderResponse> loadPreTravelOrder(@PathVariable String preOrderNo) {
        return CommonResult.success(orderService.loadPreTravelOrder(preOrderNo));
    }

    /**
     * 创建订单(旅游)
     */
    @Operation(summary = "创建订单(旅游)")
    @RequestMapping(value = "/create/travel", method = RequestMethod.POST)
    public CommonResult<Map<String, Object>> createTravelOrder(@Validated @RequestBody CreateTravelOrderRequest orderRequest) {
        return CommonResult.success(orderService.createTravelOrder(orderRequest));
    }

    /**
     * 订单列表(旅游)
     * @param type 类型
     * @param pageRequest 分页
     * @return 订单列表
     */
    @Operation(summary = "订单列表")
    @RequestMapping(value = "/list/travel", method = RequestMethod.GET)
    @Parameter(name = "type", description = "评价等级|0=未支付,1=待使用,2=已完成,-3=售后/退款", required = true, in = ParameterIn.QUERY)
    public CommonResult<CommonPage<OrderTravelDetailResponse>> orderTravelList(@RequestParam(name = "type") Integer type,
                                                                               @ModelAttribute PageParamRequest pageRequest) {
        return CommonResult.success(orderService.orderTravelList(type, pageRequest));
    }

    /**
     * 订单详情(旅游)
     * @param orderId 订单编号
     * @return 订单详情
     */
    @Operation(summary = "订单详情",description = "订单编号查询")
    @RequestMapping(value = "/detail/travel/{orderId}", method = RequestMethod.GET)
    public CommonResult<StoreTravelOrderDetailInfoResponse> orderTravelDetail(@PathVariable String orderId) {
        return CommonResult.success(orderService.orderTravelDetail(orderId));
    }

    /**
     * 订单详情查询ById(旅游)
     * @param id 订单id
     * @return 订单详情
     */
    @Operation(summary = "订单详情",description = "根据id查询")
    @RequestMapping(value = "/detail/travel/id/{id}", method = RequestMethod.GET)
    public CommonResult<StoreTravelOrderDetailInfoResponse> orderTravelDetailById(@PathVariable Long id) {
        return CommonResult.success(orderService.orderTravelDetailById(id));
    }

    /**
     * 订单取消
     * @param id Integer 订单id
     */
    @Operation(summary = "订单取消")
    @RequestMapping(value = "/cancel", method = RequestMethod.POST)
    public CommonResult<Boolean> cancel(@RequestParam(value = "id") Long id) {
        if(orderService.cancel(id)) {
            return CommonResult.success();
        }else{
            return CommonResult.failed();
        }
    }


    /**
     * 订单退款申请
     * @param request OrderRefundApplyRequest 订单id
     */
    @Operation(summary = "订单退款申请")
    @RequestMapping(value = "/refund", method = RequestMethod.POST)
    public CommonResult<Boolean> refundApply(@RequestBody @Validated OrderRefundApplyRequest request) {
        try {
            if(orderService.refundApply(request)) {
                return CommonResult.success();
            }else{
                return CommonResult.failed();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(ExceptionCodeEnum.FAILED);
        }
    }

    /**
     * 退款申请取消
     * @param id Integer 订单id
     */
    @Operation(summary = "退款申请取消")
    @RequestMapping(value = "/cancel-application", method = RequestMethod.POST)
    public CommonResult<Boolean> cancelApplication(@RequestParam(value = "id") Long id) {
        if(orderService.cancelApplication(id)) {
            return CommonResult.success();
        }else{
            return CommonResult.failed();
        }
    }

    /**
     * 删除已完成订单
     * @param id String 订单号
     * @return 删除结果
     */
    @Operation(summary = "删除订单")
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public CommonResult<Boolean> delete(@RequestParam Long id) {
        if( orderService.delete(id)) {
            return CommonResult.success();
        }else{
            return CommonResult.failed();
        }
    }

    /**
     * 订单使用
     * @param id Integer 订单id
     */
    @Operation(summary = "订单使用")
    @RequestMapping(value = "/take", method = RequestMethod.POST)
    public CommonResult<Boolean> take(@RequestParam(value = "id") Long id) {
        if(orderService.take(id)) {
            return CommonResult.success();
        }else{
            return CommonResult.failed();
        }
    }
}
